package com.zhumo.sz.service.impl;

import com.zhumo.sz.common.CommonInsert;
import com.zhumo.sz.common.CustomLogServerConfig;
import com.zhumo.sz.common.http.HttpClientHelper;
import com.zhumo.sz.common.utils.DateFormatUtil;
import com.zhumo.sz.entity.area.BaseArea;
import com.zhumo.sz.gaode.GaoDeAddressProcessor;
import com.zhumo.sz.gaode.GaoDeGeoArea;
import com.zhumo.sz.ipweb.IPSearchProcessor;
import com.zhumo.sz.ipweb.IPSearchResult;
import com.zhumo.sz.mapper.PublicMapper;
import com.zhumo.sz.service.PublicService;
import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class PublicServiceImpl implements PublicService {


    @Autowired
    private PublicMapper publicMapper;

    private static GaoDeAddressProcessor gaoDeAddressProcessor;

    /**
     * 初始化处理器
     */
    public static void InitProcessor() {
        if (gaoDeAddressProcessor != null) {
            if (gaoDeAddressProcessor.getApplicationKey().length() != 32) {
                throw new IllegalArgumentException("高德应用key传递有误");
            }
        } else {
            //默认使用注册的应用key
            gaoDeAddressProcessor = new GaoDeAddressProcessor(CustomLogServerConfig.GaoDE_APPLICATION_KEY);
        }
    }


    /**
     * 插入公共地区信息
     * todo 高德的地址编码暂时只能获取到citycode  streetcode
     * 因此优先取gd_code 高德code 如果为空 则取ipcode
     */
    @Override
    public CommonInsert<BaseArea> addBaseArea() {
        IPSearchProcessor ipSearchProcessor = new IPSearchProcessor();
        InitProcessor();
        HttpClient client = HttpClientHelper.createDefault();
        ArrayList<BaseArea> areaCollect = new ArrayList<>();
        List<IPSearchResult> ipSearchResultList = ipSearchProcessor.getAllStreets("深圳",client);
        for (IPSearchResult ipSearchResult : ipSearchResultList) {
            BaseArea baseArea = new BaseArea();
            String gdSearchKey =
                    ipSearchResult.getProvince() + ipSearchResult.getCity() + ipSearchResult.getDistrict()
                    + ipSearchResult.getTown();
            GaoDeGeoArea gaoDeGeoArea = gaoDeAddressProcessor.processGeocoAddress(gdSearchKey, "JSON");
            baseArea.setCountry(gaoDeGeoArea.getCountry());
            baseArea.setIpZipCode(ipSearchResult.getZipCode());
            baseArea.setGdCountryCode("1");
            baseArea.setIpwebCountryCode("00001");
//            baseArea.setIpwebCountryCode(ipSearchResult.getCode());
            baseArea.setProvince(gaoDeGeoArea.getProvince());
//            baseArea.setGdProvinceCode(gaoDeGeoArea.getProvinceCode();
            baseArea.setIpwebProvinceCode(ipSearchResult.getProvinceCode());
            baseArea.setProvinceZipCode(ipSearchResult.getProvinceZipCode());
            baseArea.setCity(gaoDeGeoArea.getCity());
            baseArea.setGdCityCode(gaoDeGeoArea.getCityCode());
            baseArea.setCityZipCode(ipSearchResult.getCityZipCode());
            baseArea.setCityLat(ipSearchResult.getCityLat());
            baseArea.setCityLng(ipSearchResult.getCityLng());
            baseArea.setDistrict(gaoDeGeoArea.getDistrict());
//            baseArea.setGdDistrictCode(gaoDeGeoArea.getAdCode());
            baseArea.setIpwebDistrictCode(ipSearchResult.getDistrictCode());
            baseArea.setDistrictLat(ipSearchResult.getDistrictLat());
            baseArea.setDistrictLng(ipSearchResult.getDistrictLng());
            baseArea.setTown(ipSearchResult.getTown());
            baseArea.setIpwebStreetCode(ipSearchResult.getCode());
//            baseArea.setGdStreetCode(gaoDeGeoArea.getStreet());
            baseArea.setIpwebStreetCode(baseArea.getIpwebStreetCode());
            baseArea.setLat(ipSearchResult.getLat());
            baseArea.setLng(ipSearchResult.getLng());
            baseArea.setJianPin(ipSearchResult.getJianPin());
            baseArea.setPinYin(ipSearchResult.getPinYin());
            baseArea.setEnable(true);
            baseArea.setParentCode(ipSearchResult.getParentCode());
            baseArea.setLevel(4);
            baseArea.setCreateTime(Timestamp.valueOf(DateFormatUtil.toYmdHms(System.currentTimeMillis())));
            areaCollect.add(baseArea);
        }
        int result = publicMapper.addBaseArea(areaCollect);
        return new CommonInsert<>(areaCollect.size(),result);
    }

}
